package com.cszjzx.member.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.cszjzx.member.po.Member;
import com.cszjzx.member.util.ExcelUtilsDao;
import com.cszjzx.member.util.ExcelUtilsDaoImpl;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class ImportExcelAction extends ActionSupport{
	private String process;
	private String fileName;
	 //注意，file并不是指前端jsp上传过来的文件本身，而是文件上传过来存放在临时文件夹下面的文件
    private File file;
    //提交过来的file的名字
    private String fileFileName;
	private ExcelUtilsDao excelUtilsDao;
	private String message;
	private List<Member> members;
	private List<Member> membersList;
	
	public List<Member> getMembersList() {
		return membersList;
	}

	public void setMembersList(List<Member> membersList) {
		this.membersList = membersList;
	}

	public List<Member> getMembers() {
		return members;
	}

	public void setMembers(List<Member> members) {
		this.members = members;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getProcess() {
		return process;
	}

	public void setProcess(String process) {
		this.process = process;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public String show(){
		excelUtilsDao = new ExcelUtilsDaoImpl();
		int p = Integer.parseInt(process);
		if(p == -1){
			fileName = "老团员.xls";
		}
		else if(p == 1){
			fileName = "第一阶段.xls";
		}
		else if(p == 2){
			fileName = "第二阶段.xls";
		}
		else if(p == 3){
			fileName = "第三阶段.xls";
		}
		
		System.out.println("1:"+fileName);
		excelUtilsDao.createMembersExcelTemplate(fileName);
//		try {
//			fileName = new String (fileName.getBytes(),"utf8");
//		} catch (UnsupportedEncodingException e) {
//			e.printStackTrace();
//		}
		return "createMembersExcelTemplate";
	}
	
	@SuppressWarnings({ "unused", "resource" })
	public String execute(){
		
		String realPath = ServletActionContext.getServletContext().getRealPath("upload");
		String savePath = realPath + "\\";
		InputStream is;
		try {
			if(file!=null){
				is = new FileInputStream(file);
				String[] ss = fileFileName.split("\\.");
				String format = ss[ss.length-1];
				if(!(format.equals("xls")||format.equals("xlsx"))){
					message = "上传文件格式不对";
					return "error";
				}
				OutputStream os = new FileOutputStream(new File(savePath, fileFileName));
				byte[] buffer = new byte[500];
				int length = 0;      
				while(-1 != (length = is.read(buffer, 0, buffer.length))){
		          os.write(buffer);
				}      
				os.close();
				is.close();	
				File file1 = new File(savePath+"\\"+fileFileName);
				
				excelUtilsDao = new ExcelUtilsDaoImpl();
				//需要处理的数据
				membersList = excelUtilsDao.readMembersFromExcel(file1,Integer.parseInt(process));
				for(int i=0;i<membersList.size();i++){
					System.out.println(membersList.get(i).toString());
				}
				System.out.println("process:"+process);
				if(excelUtilsDao.hasErrors()){
					message = "上传文件中以下信息有错";
					members = excelUtilsDao.getErrorMembers();
				}
				else {
					message = "上传文件成功";
					members = null;
					/*OperationRecordService operationRecordService = new OperationRecordServiceImpl();
					operationRecordService.importMemberList(master.getMasterid(), process, new Date(), fileFileName);*/
				}
			}else{
				message = "选择上传的文件！";
				return "error";
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println("file1:");
		return "importSuccess";
	}
}
